<template>
  <div class="partner-media">
    <div class="page-header">
      <div class="container">
        <h1 class="page-title">合作媒体</h1>
        <p class="page-subtitle">与众多知名媒体建立合作关系</p>
      </div>
    </div>
    <div class="container">
      <section class="media-section">
        <h2 class="section-title">合作媒体</h2>
        <div class="media-content">
          <div class="media-intro">
            <h3>媒体合作伙伴</h3>
            <p>我们与众多知名媒体建立了长期合作关系，为展会提供全方位的宣传报道支持，确保展会信息能够及时、准确地传达给目标受众。</p>
          </div>
          <div class="media-grid">
            <div class="media-item" v-for="media in partnerMedia" :key="media.id">
              <div class="media-logo">
                <img :src="media.logo" :alt="media.name">
              </div>
              <div class="media-info">
                <h4>{{ media.name }}</h4>
                <p>{{ media.type }}</p>
                <p class="media-description">{{ media.description }}</p>
              </div>
            </div>
          </div>
        </div>
      </section>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { subpageApi } from '@/api/user'

export default {
  name: 'PartnerMedia',
  setup() {
    // 页面数据
    const pageData = ref({})
    
    // 合作媒体数据
    const partnerMedia = ref([])

    // 加载合作媒体数据
    const loadPartnerMedia = async () => {
      try {
        const response = await subpageApi.getSubpageByKey('media', 'partner-media')
        console.log('合作媒体API响应:', response)
        
        if (response && response.code === '200' && response.data) {
          pageData.value = {
            title: response.data.title || '合作媒体',
            subtitle: response.data.subtitle || '与众多知名媒体建立合作关系'
          }
          
          if (response.data.content) {
            try {
              // 尝试解析JSON内容
              if (response.data.content.trim().startsWith('[') || response.data.content.trim().startsWith('{')) {
                const contentData = JSON.parse(response.data.content)
                partnerMedia.value = contentData.media || contentData || []
              } else {
                // 如果是纯文本内容，转换为结构化数据
                console.log('合作媒体数据是纯文本格式，转换为结构化数据')
                partnerMedia.value = parseTextToMedia(response.data.content)
              }
            } catch (e) {
              console.error('解析合作媒体数据失败:', e)
              partnerMedia.value = parseTextToMedia(response.data.content)
            }
          } else {
            partnerMedia.value = getDefaultPartnerMedia()
          }
        } else {
          partnerMedia.value = getDefaultPartnerMedia()
        }
      } catch (error) {
        console.error('加载合作媒体数据失败:', error)
        partnerMedia.value = getDefaultPartnerMedia()
      }
    }

    // 将纯文本内容转换为结构化的合作媒体数据
    const parseTextToMedia = (textContent) => {
      if (!textContent || typeof textContent !== 'string') return getDefaultPartnerMedia()
      const paragraphs = textContent.split('\n').map(p => p.trim()).filter(Boolean)
      if (paragraphs.length === 0) return getDefaultPartnerMedia()
      return paragraphs.map((paragraph, index) => {
        const titleMatch = paragraph.match(/^([^：:]+)[：:]/)
        const name = titleMatch ? titleMatch[1].trim() : `合作媒体${index + 1}`
        const content = titleMatch ? paragraph.slice(paragraph.indexOf('：') + 1).trim() : paragraph
        return {
          id: index + 1,
          name,
          type: '合作媒体',
          description: content,
          logo: 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=100&h=100&fit=crop'
        }
      })
    }

    // 获取默认的合作媒体数据
    const getDefaultPartnerMedia = () => {
      return [
      {
        id: 1,
        name: '科技日报',
        type: '国家级科技媒体',
        description: '中国科技领域权威媒体，专注于科技产业报道',
        logo: 'https://images.unsplash.com/photo-1540575467063-178a50c2df87?w=100&h=100&fit=crop'
      },
      {
        id: 2,
        name: '工业报',
        type: '专业工业媒体',
        description: '工业制造领域专业媒体，深度报道产业发展',
        logo: 'https://images.unsplash.com/photo-1581091226825-a6a2a5aee158?w=100&h=100&fit=crop'
      },
      {
        id: 3,
        name: '经济参考报',
        type: '经济类媒体',
        description: '权威经济媒体，关注产业经济发展动态',
        logo: 'https://images.unsplash.com/photo-1516321318423-f06f85e504b3?w=100&h=100&fit=crop'
      },
      {
        id: 4,
        name: '中国工业报',
        type: '工业专业媒体',
        description: '中国工业领域权威媒体，全面报道工业发展',
        logo: 'https://images.unsplash.com/photo-1552664730-d307ca884978?w=100&h=100&fit=crop'
      },
      {
        id: 5,
        name: '机器人网',
        type: '行业专业网站',
        description: '机器人行业专业网站，提供最新行业资讯',
        logo: 'https://images.unsplash.com/photo-1485827404703-89b55fcc595e?w=100&h=100&fit=crop'
      },
      {
        id: 6,
        name: '智能制造网',
        type: '行业专业网站',
        description: '智能制造领域专业网站，关注技术发展趋势',
        logo: 'https://images.unsplash.com/photo-1581094794329-c8112a89af12?w=100&h=100&fit=crop'
      }
    ]}

    onMounted(() => {
      loadPartnerMedia()
    })

    return {
      pageData,
      partnerMedia
    }
  }
}
</script>

<style scoped>
.partner-media {
  min-height: 100vh;
  background: linear-gradient(135deg, #320063 0%, #4a0a8a 100%);
}

.page-header {
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  padding: 120px 0 80px 0;
  margin-top: 0;
  text-align: center;
  color: white;
}

.page-title {
  font-size: 3rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-subtitle {
  font-size: 1.2rem;
  opacity: 0.9;
  margin-bottom: 0;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.media-section {
  background: white;
  border-radius: 20px;
  padding: 3rem;
  margin: 2rem 0;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
}

.section-title {
  font-size: 2rem;
  color: #333;
  margin-bottom: 2rem;
  text-align: center;
}

.media-content {
  max-width: 1000px;
  margin: 0 auto;
}

.media-intro {
  margin-bottom: 3rem;
}

.media-intro h3 {
  font-size: 1.8rem;
  color: #320063;
  margin-bottom: 1rem;
}

.media-intro p {
  color: #666;
  line-height: 1.8;
  font-size: 1.1rem;
}

.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 2rem;
}

.media-item {
  background: white;
  border-radius: 15px;
  padding: 1.5rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
  border: 1px solid #e0e7ff;
  display: flex;
  align-items: center;
  gap: 1rem;
}

.media-item:hover {
  transform: translateY(-5px);
}

.media-logo {
  width: 80px;
  height: 80px;
  border-radius: 10px;
  overflow: hidden;
  flex-shrink: 0;
}

.media-logo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.media-info h4 {
  font-size: 1.2rem;
  color: #320063;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.media-info p {
  color: #320063;
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, #f8f9ff 0%, #e8f0ff 100%);
  padding: 0.3rem 0.8rem;
  border-radius: 15px;
  display: inline-block;
}

.media-description {
  color: #666;
  line-height: 1.6;
  font-size: 0.9rem;
  margin: 0;
}

@media (max-width: 768px) {
  .media-section {
    padding: 2rem;
    margin: 1rem 0;
  }
  
  .section-title {
    font-size: 1.5rem;
  }
  
  .media-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
  
  .media-item {
    flex-direction: column;
    text-align: center;
  }
  
  .page-title {
    font-size: 2rem;
  }
  
  .page-subtitle {
    font-size: 1rem;
  }
}
</style>